define(function (require, exports, module) {
    require("menu/jquery-accordion-menu.css");
    require("menu/jquery-accordion-menu");

    /**
     * @return {boolean}
     */
    $.expr[":"].Contains = function(a, i, m) {
        return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
    };
    (function(header) {
        //@header 头部元素
        //@list 无需列表
        //创建一个搜素表单
        var list = header.parent().find("ul.top-ul");
        var form = $("<form class='filterform'><input class='filterinput' /></form>").attr({
            "class":"filterform"
        });
        $(form).appendTo(header).find("input").on("input propertychange", function(event) {
            var filter = $(this).val();
            if (filter.length) {
//                    list.find("li:hidden").slideDown();
                var $matches = list.find("a:Contains(" + filter + ")");
                if(!$matches.length){
                    list.find("li").each(function(){
                        if($(this).children("a").hasClass("submenu-indicator-minus")){
                            $(this).click();
                        }
                    }).slideUp();
                }
                var lis = $(), pa = $();
                $matches.each(function(){
                    pa = $(this).closest("li.top-li");
                    if(!pa.children("a").hasClass("submenu-indicator-minus")){
                        pa.click();
                    }
                    $(this).parentsUntil("ul.top-ul").each(function(){
                        if(this.tagName == 'LI'){
                            lis = lis.add($(this));
                        }
                    });
                });
                list.find("li").not(lis).each(function(){
                    if($(this).children("a").hasClass("submenu-indicator-minus")){
                        $(this).click();
                    }
                    $(this).slideUp();
                });
                lis.slideDown();
            }else{
                list.find("li").not(lis).each(function(){
                    if($(this).children("a").hasClass("submenu-indicator-minus")){
                        $(this).click();
                    }
                });
                list.find("li:hidden").slideDown();
            }
        });
    })($("#side-menu").parent().prev());

    $(function () {
        var sites = [];
        $("#site>a").each(function (i, item) {
            sites.push($(item).attr("data-id"));
        });
        for (var i = sites.length - 1; i >= 0; i--) {
            var target = $('ul').find("a[data-id=" + sites[i] + "]");
            if (target.length) {
                document.title = target.attr("data-title");
                var pUl = target.closest("ul");
                while(!pUl.hasClass("top-ul")){
                    target.addClass("clicked");
                    target = pUl.prev();
                    target.addClass("submenu-indicator-minus");
                    pUl.show();
                    pUl = target.closest("ul");
                }
                target.parent().addClass("active");
                break;
            }
        }
    });

    $("#sidebar-collapse").on("click", function(){
        var that = this;
        if($(this).hasClass("is-collapsing")){
            return;
        }
        $(this).addClass("is-collapsing");
        setTimeout(function(){
            $(that).removeClass("is-collapsing");
        }, 500);
        if($(this).hasClass("collapsed")){
            $(this).removeClass("collapsed");
            $(this).children().removeClass("fa-angle-double-right");
            $(this).parent().animate({width:"250px"}, function(){
                $(that).next().show().next().show();
                $("#globContent").trigger("resize");
            }).next(".graphs").animate({"marginLeft":"250px"});
        }else{
            $(that).next().hide().next().hide();
            $(this).children().addClass("fa-angle-double-right");
            $(this).addClass("collapsed");
            $(this).parent().animate({width:"16px"}, function(){
                $("#globContent").trigger("resize");
            }).next(".graphs").animate({"marginLeft":"16px"});
        }
    });
});